package com.hc.server;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Request {

	private static final String ACTION_NAME = "ACTION_NAME";
	private static final String ACTION_INFO = "ACTION_INFO";
	
	private JSONObject req;
	private JSONObject args;
	
	protected static Request createRequest(String req) throws JSONException{
		JSONObject reqObj = new JSONObject(req);
		JSONObject argsObj = reqObj.optJSONObject(ACTION_INFO);
		return new Request(reqObj, argsObj);
	}
	
	private Request(JSONObject req, JSONObject args){
		this.req = req;
		this.args = args;
	}
	
	protected String getRequestName(){
		return req.optString(ACTION_NAME);
	}
	
	public int getIntArg(String key) throws JSONException{
		return args.getInt(key);
	}
	
	public int optIntArg(String key){
		return args.optInt(key);
	}
	
	public String getStrArg(String key) throws JSONException{
		return args.getString(key);
	}
	
	public String optStrArg(String key){
		return args.optString(key);
	}
	
	public JSONArray getJSONArrayArg(String key) throws JSONException{
		return args.getJSONArray(key);
	}
	
	public JSONObject getJSONObjectArg(String key) throws JSONException{
		return args.getJSONObject(key);
	}
	
//	public Date getDateArg(String key) throws JSONException{
//		String dstr = args.getString(key);
//		return Date.valueOf(dstr);
//	}
//	
//	public Date optDateArg(String key){
//		String dstr = args.optString(key);
//		if(!dstr.isEmpty()){
//			return null;
//		}
//		return Date.valueOf(dstr);
//	}
}
